package org.realogic.cfm.persistence.mybatis.interceptor;

import org.realogic.cfm.persistence.dialect.Dialect;
import org.realogic.cfm.persistence.query.QueryCondition;

public class SQLHelp {

	/**
	 * 根据数据库方言，生成特定的分页sql
	 * 
	 * @param sql
	 *            Mapper中的Sql语句
	 * @param page
	 *            分页对象
	 * @param dialect
	 *            方言类型
	 * @return 分页SQL
	 */
	public static String generatePageSql(String sql1, QueryCondition condition, Dialect dialect) {
		String sql = generateQuerySql(sql1, condition);
		if (dialect.supportsLimit()) {
			return dialect.getLimitString(sql, condition.getPageNo(), condition.getPageSize());
		} else {
			return sql;
		}
	}

	public static String generateCountSql(String sql, QueryCondition condition, Dialect dialect) {
		return dialect.getCountSQL(generateQuerySql(sql, condition));
	}

	public static String generateQuerySql(String sql, QueryCondition condition) {
		String tmpSQL = sql;
		if (condition != null) {
			tmpSQL += condition.getWhereConditionString();
		}
		return tmpSQL;
	}
}
